package com.memory.wechat.domain.service;

import com.memory.modules.business.domain.user.Account;
import com.memory.modules.business.service.user.AccountService;
import com.memory.wechat.domain.msg.MessageType;
import com.memory.wechat.domain.response.TextMessage;
import com.memory.wechat.domain.util.ReplyMessageUtil;
import com.memory.wechat.domain.util.WxRequestUtil;
import com.memory.wechat.domain.util.wx.WxUser;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Log4j2
@Service
public class WeChatService {
    @Resource
    private AccountService accountService;

    // 处理微信发来的请求 map 消息业务处理分发
    public  String parseMessage(Map<String, String> map) {
        String respXml = null;
        try {
            // 发送方帐号
            String fromUserName = map.get("FromUserName");
            // 开发者微信号
            String toUserName = map.get("ToUserName");
            // 取得消息类型
            String MsgType = map.get("MsgType");
            // 发现直接把要返回的信息直接封装成replyMap集合，然后转换成 xml文件，是不是实体类可以不用了
            Map<String, String> replyMap = new HashMap<String, String>();
            replyMap.put("ToUserName", fromUserName);
            replyMap.put("FromUserName", toUserName);
            replyMap.put("CreateTime", String.valueOf(new Date().getTime()));
            // 发啥我都返回文本消息
            TextMessage textMessage = new TextMessage();
            textMessage.setToUserName(fromUserName);
            textMessage.setFromUserName(toUserName);
            textMessage.setCreateTime(new Date().getTime());
            textMessage.setContent("北京众存科技有限公司欢迎您");
            textMessage.getMsgType();
            respXml = ReplyMessageUtil.sendTextMessage(textMessage);
            System.out.println(">>>>返回消息："+respXml);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return respXml;
    }

    // 事件消息业务分发
    public String parseEvent(Map<String, String> map) {
        String respXml = null;
        try {
            // 发送方帐号
            String fromUserName = map.get("FromUserName");
            // 开发者微信号
            String toUserName = map.get("ToUserName");
            // 取得消息类型
            String MsgType = map.get("MsgType");
            //获取事件类型
            String eventType = map.get("Event");

            // 发现直接把要返回的信息直接封装成replyMap集合，然后转换成 xml文件，是不是实体类可以不用了
            Map<String, String> replyMap = new HashMap<String, String>();
            replyMap.put("ToUserName", fromUserName);
            replyMap.put("FromUserName", toUserName);
            replyMap.put("CreateTime", String.valueOf(new Date().getTime()));
            if (eventType.equals(MessageType.EVENT_TYPE_SUBSCRIBE)) {// 关注
                //关注事件不返回任何东西，注册用户实现
                log.info(">>>>>>>>>>>>>>有用户注册了 fromUserName："+ fromUserName+" toUserName："+toUserName);
                processUserAccount(fromUserName);
            }
            if (eventType.equals(MessageType.EVENT_TYPE_UNSUBSCRIBE)) {// 取消关注

            }
            if (eventType.equals(MessageType.EVENT_TYPE_SCAN)) {// 用户已关注时的扫描带参数二维码

            }
            if (eventType.equals(MessageType.EVENT_TYPE_LOCATION)) {// 上报地理位置

            }
            if (eventType.equals(MessageType.EVENT_TYPE_CLICK)) {// 自定义菜单

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return respXml;
    }

    private void processUserAccount(String openId) {
        try {
            WxUser wxUser = WxRequestUtil.getUserInfo(openId);
            //判断数据库中是否存在openid的数据
            Account account = accountService.findByOpenId(openId);
            if (account == null) {
                account = new Account();
                account.setOpenId(openId);

                //需要补充获取用户基本信息
                account.setNickName(wxUser.getNickName());
                accountService.create(account);
            } else {
                //更新账号信息
                account.setOpenId(openId);
                account.setNickName(wxUser.getNickName());
                accountService.modify(account.getId(),account);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
